package org.cupk2.zhzm.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.cupk2.base.db.CupkDbFields;
import org.cupk2.base.domain.CupkUserContextLoader;
import org.cupk2.base.exception.CupkExceptionUtils;
import org.cupk2.zhzm.dao.ZhzmKnowTagDao;
import org.cupk2.zhzm.dao.ZhzmTagDao;
import org.cupk2.zhzm.service.ZhzmTagServiceInter;
import org.cupk2.zhzm.domain.ZhzmKnowTag;
import org.cupk2.zhzm.domain.ZhzmTag;
import org.cupk2.zhzm.dto.web.WebTagDto;
import org.cupk2.tools.db.DataQuery;
import org.cupk2.tools.db.DataResult;
import org.cupk2.tools.db.commons.DBRule;
import org.cupk2.tools.db.commons.DBRuleList;
import org.cupk2.tools.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**系统标签 
 * @author cbtg自动生成  2025-2-11 15:32:01 
 */
@Service
@Slf4j
public class ZhzmTagServiceImpl implements ZhzmTagServiceInter {
    @Autowired
    private ZhzmTagDao zhzmTagDao;
    @Autowired
    private ZhzmKnowTagDao zhzmKnowTagDao;

    @Transactional
    @Override
    public ZhzmTag insertZhzmTagEntity(ZhzmTag zhzmTag) {
        CupkDbFields.initInsertBean(zhzmTag, CupkUserContextLoader.getCurrentUser());

        zhzmTagDao.insert(zhzmTag);


        return zhzmTag;
    }

    @Transactional
    @Override
    public ZhzmTag editZhzmTagEntity(ZhzmTag zhzmTag) {
        ZhzmTag saveZhzmTag = getZhzmTagById(zhzmTag.getId());
        CupkExceptionUtils.throwNullEx(saveZhzmTag, I18n.msg("系统标签不存在:?", zhzmTag.getId()));
        saveZhzmTag.setId(zhzmTag.getId());
        saveZhzmTag.setCtime(zhzmTag.getCtime());
        saveZhzmTag.setEtime(zhzmTag.getEtime());
        saveZhzmTag.setEuserkey(zhzmTag.getEuserkey());
        saveZhzmTag.setCuserkey(zhzmTag.getCuserkey());
        saveZhzmTag.setState(zhzmTag.getState());
        saveZhzmTag.setNote(zhzmTag.getNote());
        saveZhzmTag.setName(zhzmTag.getName());
        saveZhzmTag.setTagkey(zhzmTag.getTagkey());
        saveZhzmTag.setSortcode(zhzmTag.getSortcode());
        saveZhzmTag.setHaspop(zhzmTag.getHaspop());
        saveZhzmTag.setHastemt(zhzmTag.getHastemt());
         
        CupkDbFields.initUpdateBean(saveZhzmTag, CupkUserContextLoader.getCurrentUser());
        zhzmTagDao.update(saveZhzmTag);
        return saveZhzmTag;
    }

    @Transactional
    @Override
    public ZhzmTag getZhzmTagById(String id) {
        return zhzmTagDao.findById(id);
    }

    @Override
    public List<ZhzmTag> getZhzmTags(DataQuery query) {
        return zhzmTagDao.queryData(query.setCount(false)).getData(ZhzmTag.class);
    }



    @Transactional
    @Override
    public DataResult searchZhzmTag(DataQuery query) {
        DataResult result = zhzmTagDao.queryData(query);
        return result;
    }

    @Override
    public int getZhzmTagNum(DataQuery query) {
        return zhzmTagDao.countData(query);
    }


    @Transactional
    @Override
    public void delZhzmTag(String id) {
        /*[tree：树形结构使用]
        if ( zhzmTagDao.findByParentId(id).size() > 0) {
            throw new RuntimeException("不能删除该节点，请先删除其子节点");
        }
        */
        zhzmTagDao.deleteById(id);
    }
    
    @Override
    public int getNum(DataQuery query) {
        return  zhzmTagDao.countData(query);
    }


    /* [tree：树形结构使用]
    @Transactional
    @Override*/
    public void autoSort(List<String> ids) {
        int sort = 0;
        for (String id : ids) {
            ZhzmTag node =  zhzmTagDao.findById(id);
            if (sort == 0) {
                sort = node.getSortcode();
            }
            node.setSortcode(sort++);
            zhzmTagDao.update(node);
        }
    }

    @Override
    public List<ZhzmTag> getTags(String key) {
        DBRuleList rules = DBRuleList.getInstance();
        if (StringUtils.isNotBlank(key)) {
            rules.add(new DBRule("NAME", key, "like"));
        }
        List<ZhzmTag> tags = zhzmTagDao.find(rules.toList());
        if (tags.size() > 30) {
            return tags.subList(0, 30);
        } else {
            return tags;
        }
    }

    @Override
    public List<WebTagDto> getUserTags() {
        List<WebTagDto> list = new ArrayList<>();
        List<ZhzmTag> allTags = zhzmTagDao.findAll();
        for (ZhzmTag tag : allTags) {
            WebTagDto dto = new WebTagDto();
            dto.setType("2");
            dto.setKnowNum(getTagKnowNum(tag.getTagkey()));
            dto.setName(tag.getName());
            dto.setTagkey(tag.getTagkey());
            list.add(dto);
        }
        if (list.size() < 30) {
            List<ZhzmKnowTag> mytags = zhzmKnowTagDao.queryData(DataQuery.getInstance()//
                    .setSql("TITLE,TYPE,", "ZHZM_KNOW_TAG")//
                    .addRule(new DBRule("TYPE", "3", "="))//
                    .setPageSize(30)//
                    .setDistinct(true)).getData(ZhzmKnowTag.class);
            for (ZhzmKnowTag tag : mytags) {
                WebTagDto dto = new WebTagDto();
                dto.setType("3");
                dto.setKnowNum(getTagKnowNum(tag.getTagkey()));
                dto.setName(tag.getTitle());
                dto.setTagkey(tag.getTagkey());
                list.add(dto);
            }
        }
        return list;
    }
    private int getTagKnowNum(String tagKey) {
        int num = zhzmKnowTagDao.countData(new DataQuery().addRule(new DBRule("TAGKEY", tagKey, "=")));
        return num;
    }
}
